package com.zw.mes.module.shipments.service;


import com.zw.mes.entity.shipments.InstDelivery;
import com.zw.mes.module.assemble.entity.AsseStore;
import com.zw.mes.module.assemble.entity.AsseStoreRecord;
import com.zw.mes.module.assemble.repository.AsseStoreRecordRepository;
import com.zw.mes.module.assemble.repository.AsseStoreRepository;
import com.zw.mes.module.shipments.mapper.InstDeliveryMapper;
import com.zw.mes.module.shipments.repository.InstDeliveryRepository;
import com.zw.mes.module.shipments.repository.InstShipmentsRepository;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: liqingyu
 * @Date: 2020/11/10 15:11
 */
@Service
@SuppressWarnings("all")
public class InstDeliveryService {


    @Autowired
    private LogService logService;
    @Autowired
    private InstDeliveryMapper instDeliveryMapper;
    @Autowired
    private InstShipmentsRepository instShipmentsRepository;
    @Autowired
    private InstDeliveryRepository instDeliveryRepository;
    @Autowired
    private  AsseStoreRepository asseStoreRepository;
    @Autowired
    private AsseStoreRecordRepository asseStoreRecordRepository;

    /**
     * 查询成品库库存
     *
     * @return
     */
    public List<AsseStore> selectLinshiProductDetail(AsseStore asseStore) {
        return instDeliveryMapper.selectLinshiProductDetail(asseStore);
    }

    /**
     * 保存出库单
     *
     * @param instDelivery
     * @param prodNo
     * @param proName
     * @param drawNo
     * @param drawName
     * @param drawNum
     * @param comment
     * @param asseId
     * @param sheetId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int saveDelivery(InstDelivery instDelivery, String[] prodNo, String[] proName, String[] drawNo,
                            String[] drawName, String[] drawNum, String[] comment, String[] asseId, String[] sheetId) {
        for (int i = 0; i < asseId.length; i++) {
            //保存出库单
            InstDelivery inst= new InstDelivery();
            inst.setProdNo(prodNo[i]);
            inst.setProName(proName[i]);
            inst.setDrawNo(drawNo[i]);
            inst.setDrawName(drawName[i]);
            inst.setDrawNum(Integer.valueOf(drawNum[i]));
            if (null == comment || comment.length <= 0) {
                inst.setComment("");
            } else {
                inst.setComment(comment[i]);
            }
            inst.setAsseId(Integer.valueOf(asseId[i]));
            inst.setSheetId(Integer.valueOf(sheetId[i]));
            inst.setIsDel(0);
            inst.setCreateName(instDelivery.getCreateName());
            inst.setCreateTime(instDelivery.getCreateTime());
            inst.setDept(instDelivery.getDept());
            inst.setSubmitStatus(1);
            InstDelivery instDelivery1 = instDeliveryRepository.save(inst);
            logService.saveLog("创建出库单", "ID：" + instDelivery1.getId());
            //更新 prod_pla_ret 表 完成时间
            InstDelivery instDelivery2 = new InstDelivery();
            instDelivery2.setCreateTime(new Date());
            instDelivery2.setProdNo(instDelivery1.getProdNo());
            instDelivery2.setDept("r21");
            int a =   instDeliveryMapper.updateProdPlaRet(instDelivery2);
            //成品库出库，修改库存数量
            AsseStore asseStore = asseStoreRepository.getOne(Integer.valueOf(asseId[i]));
            asseStore.setDrawNum(asseStore.getDrawNum()-Integer.valueOf(drawNum[i]));
            asseStoreRepository.save(asseStore);
            //保存出库记录表
            AsseStoreRecord asseStoreRecord = new AsseStoreRecord();
            asseStoreRecord.setProdNo(asseStore.getProdNo());
            asseStoreRecord.setSpecial(asseStore.getSpecial());
            asseStoreRecord.setPartId(asseStore.getPartId());
            asseStoreRecord.setDrawName(asseStore.getDrawName());
            asseStoreRecord.setDrawNo(asseStore.getDrawNo());
            asseStoreRecord.setWeight(asseStore.getDrawWeight());
            asseStoreRecord.setDrawNum(Integer.valueOf(drawNum[i]));
            asseStoreRecord.setStoreType(2);
            asseStoreRecord.setRecordType(1);
            asseStoreRecord.setStoreAddr(asseStore.getStoreAddr());
            asseStoreRecord.setStorageRegisterCode(ShiroUtils.getUser().getAccount());
            asseStoreRecord.setStorage_time(new Date());
            asseStoreRecord.setAsseStoreId(asseStore.getId());
            asseStoreRecord.setInstId(instDelivery1.getId());
            AsseStoreRecord asseStoreRecord1= asseStoreRecordRepository.save(asseStoreRecord);
            logService.saveLog("成品库出库流水", "ID：" + asseStoreRecord1.getId());

        }

        for (int i = 0; i <prodNo.length ; i++) {
        }
        return 1;
    }

    /**
     * 提交页面查询
     * @param instDelivery
     * @return
     */
    public List<InstDelivery> selectInstDelivery(InstDelivery instDelivery) {
        return  instDeliveryMapper.selectInstDelivery(instDelivery);
    }

    /**
     * 冲单
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int del(Integer id) {
        //出库冲单
        InstDelivery instDelivery = instDeliveryRepository.getOne(id);
        instDelivery.setIsDel(1);
        instDelivery.setUpdateName(ShiroUtils.getUser().getName());
        instDelivery.setUpdateTime(new Date());
        InstDelivery instDelivery1=  instDeliveryRepository.save(instDelivery);
        logService.saveLog("成品库冲单", "ID：" + instDelivery.getId());
        //反写成品库表数量
        AsseStore asseStore  = asseStoreRepository.getOne(instDelivery.getAsseId());
        asseStore.setDrawNum(asseStore.getDrawNum()+instDelivery.getDrawNum());
        AsseStore asseStore1 = asseStoreRepository.save(asseStore);
        logService.saveLog("成品库反写数量", "ID：" + asseStore.getId()+"-反写数量："+ instDelivery.getDrawNum());
        //成品库流水
        List<AsseStoreRecord> asseStoreRecords = instDeliveryMapper.selectAsseStoreRecordByinstId(instDelivery.getId());
        for (int i = 0; i <asseStoreRecords.size() ; i++) {
            AsseStoreRecord asseStoreRecord2= new AsseStoreRecord();
            AsseStoreRecord asseStoreRecord = asseStoreRecordRepository.getOne(asseStoreRecords.get(i).getId());
            asseStoreRecord2.setRecordType(2);
            asseStoreRecord2.setInstId(asseStoreRecord.getInstId());
            asseStoreRecord2.setAsseStoreId(asseStoreRecord.getAsseStoreId());
            asseStoreRecord2.setReplaceId(asseStoreRecord.getId());
            asseStoreRecord2.setStorage_time(new Date());
            asseStoreRecord2.setStorageRegisterCode(ShiroUtils.getUser().getAccount());
            asseStoreRecord2.setStoreAddr(asseStoreRecord.getStoreAddr());
            asseStoreRecord2.setStoreType(2);
            asseStoreRecord2.setDrawNum(asseStoreRecord.getDrawNum());
            asseStoreRecord2.setWeight(asseStoreRecord.getWeight());
            asseStoreRecord2.setDrawNo(asseStoreRecord.getDrawNo());
            asseStoreRecord2.setDrawName(asseStoreRecord.getDrawName());
            asseStoreRecord2.setPartId(asseStoreRecord.getPartId());
            asseStoreRecord2.setSpecial(asseStoreRecord.getSpecial());
            asseStoreRecord2.setProdNo(asseStoreRecord.getProdNo());
            asseStoreRecord2.setCheckOpinion(asseStoreRecord.getCheckOpinion());
            asseStoreRecord2.setRemark(asseStoreRecord.getRemark());
            AsseStoreRecord asseStoreRecord1 =   asseStoreRecordRepository.save(asseStoreRecord2);
            logService.saveLog("成品库流水冲单记录", "ID：" + asseStoreRecord1.getId());
        }
        return  1;
    }
}
